from flask_sqlalchemy import *
from flask import Flask

app = Flask(__name__)

'''配置数据库'''
app.config['SECRET_KEY'] = 'hard to guess'  # 一个字符串，密码。也可以是其他如加密过的

# 在此登录的是root用户，要填上密码如123456，MySQL默认端口是3306。并填上创建的数据库名如youcaihua
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@127.0.0.1:3306/nft'

# 设置下方这行code后，在每次请求结束后会自动提交数据库中的变动
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

db = SQLAlchemy(app)  # 实例化数据库对象，它提供访问Flask-SQLAlchemy的所有功能

'''
使用sqlacodegen可以自动生成model，不用手动声明，但只支持sqlalchemy 而非 flask_sqlalchemy
下次可以直接改用但只支持sqlalchemy，就无须手动定义model了（哭）
'''


class Risk(db.Model):
    __tablename__ = 'risk'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50))


class NFT(db.Model):
    __tablename__ = 'nft'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(512))
    author = db.Column(db.String(100))
    date = db.Column(db.Date)
    amount = db.Column(db.Integer)
    type = db.Column(db.String(50))
    collection = db.Column(db.String(100), default="")
    platform = db.Column(db.String(100))
    blockchain = db.Column(db.String(100), default="")
    price = db.Column(db.Numeric)
    contract_address = db.Column(db.String(100), default="")
    token_id = db.Column(db.String(100), default="")
    token_std = db.Column(db.String(50), default="")
    content_url = db.Column(db.String(512), default="")
    description = db.Column(db.Text, default="")
    token_id_link = db.Column(db.String(512), default="")
    contract_address_link = db.Column(db.String(512), default="")
    origin_link = db.Column(db.String(512), default="")
    add_time = db.Column(db.DateTime)

    def to_dict(self):
        _dict = {
            "id": self.id,
            "name": self.name,
            "author": self.author,
            "date": str(self.date),
            "amount": self.amount,
            "type": self.type,
            "collection": self.collection,
            "platform": self.platform,
            "blockchain": self.blockchain,
            "price": float(self.price),
            "contract_address": self.contract_address,
            "token_id": self.token_id,
            "token_std": self.token_std,
            "content_url": self.content_url,
            "description": self.description,
            "token_id_link": self.token_id_link,
            "contract_address_link": self.contract_address_link,
            "origin_link": self.origin_link,
            "add_time": str(self.add_time)
        }
        return _dict


class Association(db.Model):
    __tablename__ = 'association'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    nft_id = db.Column(db.Integer, db.ForeignKey('nft.id'))
    risk_id = db.Column(db.Integer, db.ForeignKey('risk.id'))
    probability = db.Column(db.Integer)
    modify_time = db.Column(db.DateTime)

# if __name__ == "__main__":
# print(NFT.query.first().__dict__)
# nft = NFT(name="pythonTest",
#           author="MrBeanC",
#           date=date(2022, 7, 2),
#           amount=6,
#           type="image",
#           collection="TA",
#           platform="Bili",
#           price=60,
#           content_url=".....")
# db.session.add(nft)
# db.session.commit()
